package com.ruozedata.hadoop;

import java.io.*;

public class SerDeApp {

    public static void main(String[] args) throws Exception {
        Person person = new Person(1, "若泽", 100, 31);
        // ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data/person.obj"));
        // out.writeObject(person);
        // out.close();

        // ObjectInputStream in = new ObjectInputStream(new FileInputStream("data/person.obj"));
        // Person p = (Person)in.readObject();
        // System.out.println(p);

        DataOutputStream out = new DataOutputStream(new FileOutputStream("data/person2.obj"));
        out.writeInt(person.getId());
        out.writeUTF(person.getName());

        DataInputStream in = new DataInputStream(new FileInputStream("data/person2.obj"));
        final int id = in.readInt();
        final String name = in.readUTF();
    }
}

class Person implements Serializable {
    private int id;
    private String name;
    private double salary;
    private int age;

    public Person() {
    }

    public Person(int id, String name, double salary, int age) {
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                ", age=" + age +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
